package com.pan.admin.module.loanProduct.controller;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.thread.LockExecutor;
import com.pan.admin.common.annotation.Log;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.AjaxResult;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.common.enums.BusinessType;
import com.pan.admin.module.loanProduct.entity.LoanProductPlanField;
import com.pan.admin.module.loanProduct.entity.ProductFieldVo;
import com.pan.admin.module.loanProduct.entity.vo.ProductFieldAndFieldValueVo;
import com.pan.admin.module.loanProduct.service.LoanProductPlanFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 产品机构，资质字段匹配Controller
 *
 * @author ts
 * @date 2023-09-15
 */
@RestController
@RequestMapping("/product/loanProductPlantField")
public class LoanProductPlanFieldController extends BaseController {

    @Autowired
    private LoanProductPlanFieldService LoanProductPlanFieldService;

    /**
     * 获取已选择的机构计划字段，资质字段匹配列表
     */
    @PreAuthorize("@ss.hasPermi('product:LoanProductPlanField:list')")
    @GetMapping("/list")
    public TableDataInfo list(LoanProductPlanField loanProductPlanField) {
        startPage();
        List<LoanProductPlanField> list = LoanProductPlanFieldService.list(loanProductPlanField);
        return getDataTable(list);
    }

    /**
     * 获取-所有字段和所属字段值列表
     */
    @PreAuthorize("@ss.hasPermi('product:LoanProductPlanField:list')")
    @GetMapping("/listFieldAndValueList")
    public TableDataInfo listFieldAndValue(LoanProductPlanField field) {
        List<ProductFieldVo> list = LoanProductPlanFieldService.listFieldAndValue(field);

        //  临时过滤， 过滤掉API表单资质（有type代表机构计划资质， 否则代表api）
        if (field.getType() != null) {
            CollUtils.filter(list, t -> t.getFieldName().contains("API_"));
        }
        return getDataTable(list);
    }

    /**
     * 获取产品机构，资质字段匹配详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:LoanProductPlanField:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(LoanProductPlanFieldService.getById(id));
    }

    /**
     * 新增产品机构，资质字段匹配
     */
    @PreAuthorize("@ss.hasPermi('product:LoanProductPlanField:add')")
    @Log(title = "新增机构计划字段", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<LoanProductPlanField> loanProductPlanField) {
        Integer productId = CollUtils.findFirstNotNull(loanProductPlanField, t -> t.getLoanProductId());

        if (productId == null) {
            return toAjax(0);
        }

        return LockExecutor.executeWriteLockFormReadWrite("add:insert:" + productId, () ->
            toAjax(LoanProductPlanFieldService.insertLoanProductPlanField(loanProductPlanField))
        );
    }

    /**
     * 修改产品机构，资质字段匹配
     */
    @PreAuthorize("@ss.hasPermi('product:LoanProductPlanField:edit')")
    @Log(title = "修改机构计划字段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoanProductPlanField loanProductPlanField) {
        return toAjax(LoanProductPlanFieldService.updateLoanProductPlanField(loanProductPlanField));
    }

    /**
     * 删除产品机构，资质字段匹配
     * TODO 需要改造
     */
    @PreAuthorize("@ss.hasPermi('product:LoanProductPlanField:remove')")
    @Log(title = "删除机构计划字段", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody List<ProductFieldAndFieldValueVo> productList) {
        Integer productId = CollUtils.findFirstNotNull(productList, t -> t.getLoanProductId());

        if (productId == null) {
            return toAjax(0);
        }

        return LockExecutor.executeWriteLockFormReadWrite("add:remove:" + productId, () ->
            toAjax(LoanProductPlanFieldService.batchDeleteByListIds(productList))
        );
    }

}
